/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


//import com.classnet.ClassNet;
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Derek
 */
public class NewPostServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
   
    //private ClassNet classnet = ClassNet.INSTANCE;
    private String courseSubject; // the four letter thing, eg CPSC
    private String courseCode;  // eg 410
    
    
    String[] ngroupSections = {"Homework","Exams","General"}; // TODO pull from DB
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();
         //out.println(courseSubject + " " + courseCode);
         try {
             
            out.println("<html>");
            out.println("<head>");
            out.println("<title>News Group New Post</title>"); 
            
            //TODO : make a new CSS
            CSSGenerator.generateNewsGroupPopupReplayInlineCSS(out);
            
            out.println("</head>");
            out.println("<body>");
            
            
            
            out.println(
            //"<form>" +
            "<center>" +
            "<table height=\"380\" width=\"480\" border=\"0\">" +// style=\"border-color:#336699 \" border=\""+ngroupOuterTableBorderWidth+"\">" +
            "<tr height=\"10\">" +
            "   <td  colspan=\"2\">" +
            "       <p class=\"ngroupMessageLabel\"> " +
            "       Post Section" +     // title
            "       </p>"  +
            "   </td>" +
           
            "   <td colspan=\"2\" align=\"right\">" +
            "<form name=\"newPostForm\" method=\"POST\" action=\"TempServlet?newPost=true" +
            "&courseSubject=" + courseSubject + "&courseCode=" + courseCode +
            "\">" +
            //"   <input type=\"text\" size=\"72\" name=messageTitle readonly value=\" " +
            "     <select size=\"1\" name=\"sectionSelection\">" 
            );
            
            //generating options:
            for(int i=0; i<ngroupSections.length;i++)
            {   
                out.println("<option ");
                  // for smart defauls....
                //if(request.getParameter("sections") != null &&
                //   request.getParameter("sections").compareToIgnoreCase(ngroupSections[i])==0)
               // {
               //     out.println(" selected ");
               // }
                out.println(" value =\"" + ngroupSections[i] + "\"> ");
                out.println(ngroupSections[i]);// +  request.getParameter("sections") + " " + ngroupSections[i]);
                out.println("</option>");
                
            }     
            out.println("</select>");
      
            out.println(        
          
            "   </td>" +
            "</tr>" +
            
                    
            "<tr height=\"10\" >" +
            "   <td colspan=\"4\">" +
            "       <p class=\"ngroupMessageLabel\"> " +
            "       New Post Title" +     // Message title
            "       </p>"  +
            "   </td>" +
            "<tr height=\"10\">" +
            "   <td colspan=\"4\">" +
            "   <input type=\"text\" size=\"72\"  name=newPostTitle >" +
            "   </td>" +
            "</tr>" +
            
            "</tr>"+
             
        
            // Message body
             "<tr height=\"10\" >" +
            "   <td colspan=\"4\">" +
            "       <p class=\"ngroupMessageLabel\"> " +
            "       New Post Body" +     // message body
            "       </p>"  +
            "   </td>" +
            "</tr>"+
            "<tr height=\"40\">" +
            "   <td valign=\"top\" colspan=\"4\">" +
            "   <textarea rows=\"15\" cols=\"56\" " +        
            "    name=newPostBody >" +
            "</textarea>" +
            "   </td>" +
            "</tr>" +
                    
            "<tr height=\"7\" valign=\"top\">" +          //  |
            "   <td width=\"120\" valign=\"middle\">" +   //  | these two needs to be sorted out
            //"   &nbsp" +
            "   <p class=\"copyRight\"> ClassNet© 2007  </p>"  +
            "   </td>" +
            "   <td width=\"120\">&nbsp</td>" +
            "   <td width=\"200\" align =\"right\" valign=\"top\" >"+
           // "<form name=\"replyForm\" method=\"POST\" action=\"PostServlet\">" +
            " <input type=\"submit\" name=\"newPost\" value=\"Post\" >" +//onClick=\"window.close()\">" +
            "</form>" +  // end of the form
          //  "   <button>Reply</button>"+  
            "   </td>" +
            "   <td width=\"40\" align =\"right\" valign=\"top\">"+
            "   <button onclick=\"window.close()\">Cancel</button>"+  
            "   </td>" +                    
            "</tr>" +// +
            
            "</table>"+
            "</center>"
            
            );
         //*/   
           
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    } 
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        courseSubject = request.getParameter("courseSubject");
        courseCode = request.getParameter("courseCode");
        
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
